package com.gopay.bis.service.bank.autoroute.impl;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.gopay.common.domain.bis.RouteMessage;
import com.gopay.remote.bank.autoroute.RouteAlarm;
import com.gopay.sms.client.GopaySMSHttpClient;
import com.gopay.sms.common.constants.SMSAppId;
import com.gopay.sms.common.domain.RequestVO;
import com.gopay.sms.common.domain.ResponseVO;

/**
 * 自动路由短信报警实现
 * 
 * @SmsRouteAlarm.java
 * @author zhengxue
 * @2016-10-8 下午3:50:22 www.gopay.com.cn Inc.All rights reserved.
 */
@Service(value = "smsRouteAlarm")
public class SmsRouteAlarm implements RouteAlarm {
    private final static Logger logger = LoggerFactory.getLogger(SmsRouteAlarm.class);
    private static final String SWITCH_ON = "on";
    @Resource(name = "smsConfig")
    private Map<String, String> smsConfig;

    @Override
    public void bankRouteAlarm(RouteMessage routeReq) {
        try {
            if (SWITCH_ON.equals(smsConfig.get("switch"))) {
                // 自定义短信内容，不提供验证码校验功能
                RequestVO req = new RequestVO();
                req.setAppId(SMSAppId._90000.value);
                req.setMobile(smsConfig.get("routemobile")); // 电话号码(多个以逗号分隔)
                req.setContent(routeReq.getSpecial() + " IP:" + getLocalIp());
                ResponseVO resp = GopaySMSHttpClient.getInstance().createVerificationCode(req);

                if (resp != null) {
                    if ("100".equals(resp.getStatusCode())) {
                        logger.error("银行【" + routeReq.getBankCode() + ":" + routeReq.getGopayOrderId()
                                + "】自动路由预警短信发送成功");
                    } else {
                        logger.error("银行【" + routeReq.getBankCode() + ":" + routeReq.getGopayOrderId()
                                + "】自动路由预警短信发送失败，原因：" + resp.getMessage());
                    }
                }
            } else {
                logger.error("自动路由预警短信发送已关闭，预警信息: 银行" + routeReq.getBankCode() + "订单号：" + routeReq.getGopayOrderId()
                        + " 未获取到银行产品通道");
            }
        } catch (Exception e) {
            logger.error("SmsRouteAlarm bankRouteAlarm error: ", e);
        }
    }

    // 获取IP地址
    public static String getLocalIp() throws Exception {
        Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;
        while (allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
            // System.out.println(netInterface.getName());
            Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                ip = (InetAddress) addresses.nextElement();
                if (ip != null && ip instanceof Inet4Address) {
                    logger.error("SmsRouteAlarm本机的IP = " + ip.toString());
                    return ip.toString();
                }
            }
        }
        return null;
    }

}
